package net.jumpengine.bean;

import javafx.scene.canvas.GraphicsContext;

/**
 * 基本的素材类,重写了equals/comparaTo/hashCode/toString 方法
 * <br /><br />
 * <b>作者:&nbsp;</b>张天文 <br />
 * <b>日期:&nbsp;</b>2020年2月24日 下午4:37:49<br />
 * @date
 */
public abstract class Base implements Comparable<Base>{
	protected int x,y,width,height,index;
	protected boolean visible=true;
	
	/**
	 * 默认index为0的构造方法
	 * @param x x坐标
	 * @param y y坐标
	 * @param width 宽度
	 * @param height 高度
	 */
	public Base(int x, int y, int width, int height) {
		this(x,y,width,height,0);
	}

	/**
	 * 全部属性的构造方法
	 * @param x x坐标,层级相同,x越小越先绘制
	 * @param y y坐标,层级与x相同,y越小越先绘制
	 * @param width 宽度
	 * @param height 高度
	 * @param index 层级,数值越小越先绘制
	 */
	public Base(int x, int y, int width, int height, int index) {
		super();
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		this.index = index;
	}
	
	/**
	 * 开发者需实现的绘制方法
	 * @param g JavaFx的绘制类
	 */
	public abstract void draw(GraphicsContext g);

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}
	
	public boolean isVisible() {
		return visible;
	}
	public void setVisible(boolean visible) {
		this.visible = visible;
	}
	
	@Override
	public int compareTo(Base o) {
		int res=index-o.index;
		if(res==0) {
			res=x-o.x;
			if(res==0) {
				return y-o.y;
			}
		}
		return res;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + height;
		result = prime * result + index;
		result = prime * result + width;
		result = prime * result + x;
		result = prime * result + y;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) return true;
		if (obj == null) return false;
		if (getClass() != obj.getClass()) return false;
		Base other = (Base) obj;
		if (index != other.index) return false;
		if (height != other.height) return false;
		if (width != other.width) return false;
		if (x != other.x) return false;
		if (y != other.y) return false;
		return true;
	}
	
	@Override
	public String toString() {
		StringBuilder sbr=new StringBuilder("{net.jumpengine.bean.Base}x="); 
		sbr.append(x).append(",y=").append(y).append(",width=").append(width)
		.append(",height=").append(height).append(",index=").append(index);
		return sbr.toString();
	}
}
